$(function () {
    var defaultColumns = initColumn();
    var searchParams = {};
    vm.table = new BSTable('roleTable', "/platform/role/list", defaultColumns,searchParams);
    vm.table.setPaginationType("server");
    vm.table.init();
    // jsTree
    $('#menu-tree').on('changed.jstree',function (e,data) {
        vm.role['menus'] = data.selected;
    });
    vm.menus();
});
function initColumn () {
    return [
        {field: 'selectItem', radio: true},
        {title: '角色ID', field: 'roleId', align: 'center', valign: 'middle',width:'50px'},
        {title: '角色', field: 'roleName', align: 'center', valign: 'middle'},
        {title: '说明', field: 'remark', align: 'center', valign: 'middle'},
        {title: '创建日期', field: 'createDate', align: 'center', valign: 'middle'},
        {title: '创建人', field: 'fromAdminId', align: 'center', valign: 'middle'}
    ];
};

var vm = new Vue({
    el:"#app",
    data:{
        showList:true,
        isUpdate:false,
        role:{},
        title:"",
        table:null,
        roleMenus:[],
        checkMenu:[],
    },
    methods:{
        add:function () {
            vm.isUpdate = false;
            vm.showList = false;
            vm.role = {};
            vm.title = "新增角色";
            $('#menu-tree').jstree().deselect_all();
            // vm.menus(null);
            // vm.roleMenus(data.roleId);
        },
        update:function () {
            var data = $.getSelectData("roleTable");
            if (!data)
                return;
            vm.role = data;
            vm.isUpdate = true;
            vm.showList = false;
            vm.title = "修改角色";
            $('#menu-tree').jstree().deselect_all();
            // vm.menus(data.roleId);
            vm.loadRoleMenus(data.roleId);
        },
        del:function () {
            var data = $.getSelectData("roleTable");
            if (!data)
                return;
           window.confirm("确定要删除此条数据么？",function () {
               $.getJSON('/platform/role/delete/'+data.roleId,function (result) {
                   if (result.code == 200){
                       alert('删除成功');
                       vm.table.refresh();
                   }else
                       alert(result.msg);
               });
           });
        },
        loadRoleMenus:function (roleId) {
            $.getJSON('/platform/role/roleMenus/'+roleId,function (result) {
                if (result.code == 200){
                    $('#menu-tree').jstree().select_node(handelMenu(result.data));
                }else
                    alert(result.msg);
            });
        },
        saveOrUpdate:function () {
            var instance = $('#menu-tree').jstree('get_selected');
            vm.role['menus'] = instance;
            var urlStr = vm.isUpdate ? '/platform/role/update':'/platform/role/add';
            $.ajax({
                url:urlStr,
                type:'POST',
                dataType:'JSON',
                data:JSON.stringify(vm.role),
                contentType:'application/json',
                success:function (result) {
                    if (result.code == 200){
                        alert('操作成功');
                        vm.showList = true;
                        vm.table.refresh();
                    }else
                        alert(result.msg);
                },
                error:function (e) {
                    console.log("request error",e);
                    alert('请求失败');
                }
            })
        },
        cancel:function () {
            vm.showList = true;
        },
        menus:function () {
            $.ajax({
                url:'/platform/menu/adminMenu',
                dataType:'JSON',
                success:function (result) {
                    if (result.code == 200){
                        var menus = handelMenu(result.menuTree);
                        $('#menu-tree').jstree({
                            "core":{
                                "data":menus
                            },
                            "checkbox" : {
                                "keep_selected_style" : false
                            },
                            "plugins" : [ "wholerow", "checkbox" ]
                        });
                        // if (roleId != null)
                        //     vm.loadRoleMenus(roleId);
                    }else
                        alert(result.msg);
                },
                error:function (e) {
                    console.log("request error",e);
                    alert("请求失败");
                }
            })
        }
    }
});